package hluck.me.srorage.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hluck.me.srorage.sys.domain.SysRole;
import hluck.me.srorage.sys.mapper.SysRoleMapper;
import hluck.me.srorage.sys.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Override
    public boolean removeById(Serializable id) {
        //根据角色id删除sys_permission_role
        this.getBaseMapper().deleteRolePermissionByRid(id);
        //根据角色ID删除sys_permission_user
        this.getBaseMapper().deleteRoleUserByRid(id);
        return super.removeById(id);
    }
    /**
     * 根据角色id查询当前角色拥有的所有的权限或菜单ID
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getAllPid(Integer roleId) {
        return this.getBaseMapper().getAllPid(roleId);
    }

    /**
     * 保存角色和菜单之间的关系
     * @param id 角色id
     * @param ids 权限id
     */
    @Override
    public void saveRolePermission(Integer rid, Integer[] ids) {
        SysRoleMapper roleMapper = this.getBaseMapper();
        //根据rid删除sys_role_permission
        roleMapper.deleteRolePermissionByRid(rid);
        if (ids!=null&&ids.length>0){
            for (Integer pid : ids) {
                roleMapper.saveRolePermission(rid,pid);
            }
        }

    }

    /**
     * 根据用户id查询角色并选中已拥有的角色
     * @param id
     * @return
     */
    @Override
    public List<Integer> queryUserRoleIdsByUid(Integer id) {
        return this.getBaseMapper().queryUserRoleIdsByUid(id);
    }

    /**
     * 根据角色id查询角色已拥有的权限
     * @param id
     * @return
     */
    @Override
    public List<Integer> queryRolePermissionIdsByRid(Integer id) {
        return this.getBaseMapper().queryRolePermissionIdsByRid(id);
    }


}
